package adapters;

import java.text.ParseException;
import java.util.Date;


public class DateFormat{
	public int preMonth() {
		Date date = new Date();
		return preMonth(date);
	}

	public int preMonth(String currentDate) throws ParseException {
		Date date = checkDateStatus(currentDate);
		return preMonth(date);
	}

	public int preMonth(Date currentDate) {
		int month = currentDate.getMonth();
//		int year = currentDate.getYear();
//		year = (month == 0 ? (year - 1) : year);
		month = (month == 0 ? 11 : (month - 1));
//		return new Date(year, month, currentDate.getDate());
		return month + 1;
	}

	public int nextMonth() {
		Date date = new Date();
		return nextMonth(date);
	}

	public int nextMonth(String currentDate) throws ParseException {
		Date date = checkDateStatus(currentDate);
		return nextMonth(date);
	}
	
	public int nextMonth(Date currentDate){
		int month = currentDate.getMonth();
//		int year = currentDate.getYear();
//		year = (month == 11 ? (year + 1) : year);
		month = (month == 11 ? 0 : (month + 1));
//		return new Date(year, month, 1);
		return month + 1;
	}

	java.text.SimpleDateFormat sdf = null;//new java.text.SimpleDateFormat("yyyyMMdd");
	private Date checkDateStatus(String currentDate) throws ParseException {
		if(currentDate == null){
			throw new NullPointerException("param is null");
		}
		if(currentDate.length() != 8){
			throw new IllegalArgumentException("param status is error");
		}
		sdf = currentDate.indexOf("-") > 0?new java.text.SimpleDateFormat("yyyy-MM-dd") : new java.text.SimpleDateFormat("yyyyMMdd");
		Date date = sdf.parse(currentDate);
		return date;
	}
	
	public boolean isLeapYear(int year){
		if((year%4==0&&year%100!=0)||year%400==0){
			return true;
		}
		return false;
	}

}
